import json
import requests
import numpy as np
import calendar
import time

header = {
	"Host": "stock.xueqiu.com",
	"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0",
	"Accept": "application/json, text/plain, */*",
	"Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
	"Accept-Encoding": "gzip, deflate, br",
	"Referer": "https://xueqiu.com/S/SZ000338",
	"Origin": "https://xueqiu.com",
	"Connection": "keep-alive",
	"Cookie": "Hm_lvt_1db88642e346389874251b5a1eded6e3=1636699363,1637637468,1638105743,1638258617; device_id=f22a9f054403c0c6987cc97218f511d7; s=cp19vaih1o; bid=12f6cbd38037025574fa6c0b4111476f_kqaaqsph; xq_a_token=e8bc5b27c7b4a2e2bb256b92fce058a36b96467e; xqat=e8bc5b27c7b4a2e2bb256b92fce058a36b96467e; xq_r_token=9bc5678a1fb1f8e15e1e679212ce7f4d5845d4cc; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTY0MTA2MTE5NSwiY3RtIjoxNjM4NDk0ODY0NTI1LCJjaWQiOiJkOWQwbjRBWnVwIn0.MJNTBcStEtKpCyKErBnil5TAwJ0BnhHBme8T950w1HkKRDksB-M3Dxt5fwUyUqEjWOLCQlMpUvptOl1VIkBVL8EMGehQQ-mCxP6EIWNJMoZE_NBr2I9lBN5qsV4encKyIrllSqHZBoA9OcVPCyOFuTM6takDpoQwoaGk3XErn2iOhD0M2dh2x9EJ0OhAhBVACNuN7v9GxUqc6dPg4Cw0otFR0U2GmJylQ_UZqQQQNttGlmCFMfotS2d58Y1PKJ-DnaLo3-IIr5ZlUyu7_qKn_2o1Q5Mxd17vDsP-bVF4d0qka8WKw3JUCHO6efBOKnhxrCVGoWKV0Y1G0DiyOuiJLw; u=311638494909809; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1638494929; is_overseas=0",
	"Sec-Fetch-Dest": "empty",
	"Sec-Fetch-Mode": "cors",
	"Sec-Fetch-Site": "same-site",
	"Pragma": "no-cache",
	"Cache-Control": "no-cache"
}

def get_trade(code_name):
    t = time.time()
    t_1 = int(round(t * 1000))

    # url = 'https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=' + code_name + '&begin=1626757885739&period=day&type=before&count=-284&indicator=kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance'
    url = 'https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=' + code_name + '&begin=' + str(t_1) + '&period=day&type=before&count=-284&indicator=kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance'
    resp = requests.get(url, timeout=30, headers=header)
    res = str(resp.text)
    data = json.loads(res)
    # print(data)
    day_list = data['data']['item']

    # print(day_list)
    # print(code_name)
    div8 = 0
    div15 = 0
    div30 = 0
    today_gain = 0
    today_price = 0
    trade = []  # 交易量
    if len(day_list) > 30:
        for day in range(len(day_list) - 1, len(day_list) - 31, -1):
            if day == len(day_list) - 1:
                today_gain = float(day_list[day][7])
                today_price = float(day_list[day][5])
            trade.append(int(day_list[day][1] / 100))
        aver_8 = np.mean(trade[1:9])
        aver_15 = np.mean(trade[1:16])
        aver_30 = np.mean(trade[1:31])
        today_trade = trade[0]
        div8 = float(today_trade)/aver_8
        div15 = float(today_trade)/aver_15
        div30 = float(today_trade) / aver_30
        # print(div8, div15, div30, today_gain)
    return div8, div15, div30, today_gain, today_price


if __name__ == '__main__':
    get_trade('SZ300760')

